package com.xinz.Android.MyFlingGallery;


import com.xinz.Android.Tool.MyGraphicMethod;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyExpandableListView extends ExpandableListView implements View.OnClickListener, AbsListView.OnScrollListener{

	private float m_DeviceWidthPixel;
	private float m_DeviceHeightPixel;
	
	private float IMAGE_SIZE_SCALE_GROUP=(float) 0.05;
	@Override
	protected void dispatchDraw(Canvas canvas) {
		Log.i("ccc","dispatchDraw Start");
		super.dispatchDraw(canvas);
		Log.i("ccc","dispatchDraw end");
	}

	/**
	 * @param context
	 */
	public MyExpandableListView(Context context,DisplayMetrics dm) {
		super(context);
		mContext = context;
		m_DeviceWidthPixel= dm.widthPixels*dm.density;
		m_DeviceHeightPixel=dm.heightPixels*dm.density;
	    this.isFixGroup = true;
	   
	    super.setOnScrollListener(this);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public MyExpandableListView(Context context, AttributeSet attrs,DisplayMetrics dm) {
		super(context, attrs);
		mContext = context;
		m_DeviceWidthPixel= dm.widthPixels*dm.density;
		m_DeviceHeightPixel=dm.heightPixels*dm.density;
	    this.isFixGroup = true;

	    super.setOnScrollListener(this);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public MyExpandableListView(Context context, AttributeSet attrs, int defStyle,DisplayMetrics dm) {
		super(context, attrs, defStyle);
		mContext = context;
		m_DeviceWidthPixel= dm.widthPixels*dm.density;
		m_DeviceHeightPixel=dm.heightPixels*dm.density;
	    this.isFixGroup = true;

	    super.setOnScrollListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if(isFixGroup){//是否显示固定组头
			 boolean isFrameLayoutParent = getParent() instanceof FrameLayout;
			 mAdapter = this.getExpandableListAdapter();
			 if(isFrameLayoutParent){//当前listview是放到FrameLayout的容器中
				parentLayout = (FrameLayout)getParent();
				MyExpandableListView listView = (MyExpandableListView)view; //得到当前listview
				if(null==indicatorGroup){
					indicatorGroup = new LinearLayout(mContext);
//					indicatorGroup.setBackgroundColor(Color.DKGRAY);
					indicatorGroup.setLayoutParams(new MarginLayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
				}
				 
				int npos = view.pointToPosition(0,0);
				if(npos != AdapterView.INVALID_POSITION){
					long pos = listView.getExpandableListPosition(npos);
					int childPos = ExpandableListView.getPackedPositionChild(pos);
					int groupPos = ExpandableListView.getPackedPositionGroup(pos);
					if(childPos == AdapterView.INVALID_POSITION){
						View groupView = listView.getChildAt(npos - listView.getFirstVisiblePosition());
						indicatorGroupHeight = groupView.getHeight();	
//						if(indicatorGroup!=null){
//							indicatorGroup.setLayoutParams(new MarginLayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
//						}
					}
					// get an error data, so return now
					if(indicatorGroupHeight == 0){
						return;
					}
					// update the data of indicator group view
					if(groupPos != indicatorGroupId){				
						Log.e("bbb","ExListView : " + "bind to new group,group position = " + groupPos);
						if(mAdapter!=null){
							FixGroupView= mAdapter.getGroupView(groupPos,this.isGroupExpanded(groupPos),null, indicatorGroup);				
							indicatorGroupId = groupPos;
//							FixGroupView.setBackgroundColor(Color.RED);
//							((MyExpandableListAdapter)mAdapter).hideGroup(indicatorGroupId); // we set this group view to be hided				
//							((MyExpandableListAdapter)mAdapter).notifyDataSetChanged();//  .notifyDataSetChanged();							
						}
					}
				}
				if(indicatorGroupId == -1){
					return;
				}
				
				/**
				 * calculate point (0,indicatorGroupHeight)
				 */	
				int showHeight = indicatorGroupHeight;
				int nEndPos = listView.pointToPosition(0,indicatorGroupHeight);
				if(nEndPos != AdapterView.INVALID_POSITION){
					long pos = listView.getExpandableListPosition(nEndPos);
					int groupPos = ExpandableListView.getPackedPositionGroup(pos);
					if(groupPos != indicatorGroupId){				
						View viewNext = listView.getChildAt(nEndPos-listView.getFirstVisiblePosition());
						showHeight = viewNext.getTop();	
					}
				}	
				if(FixGroupView.getParent()==null){
					indicatorGroup.removeAllViews();
					
//					if(indicatorGroupId!=0)
//					{
					indicatorGroup.addView(FixGroupView);
//					}
				}
				
				// update group position
				ViewGroup.LayoutParams vlparams = indicatorGroup.getLayoutParams();
				if((null!=vlparams)&&(vlparams instanceof MarginLayoutParams)){
					MarginLayoutParams layoutParams = (MarginLayoutParams)vlparams;
					layoutParams.topMargin = -(indicatorGroupHeight-showHeight);
					Log.i("bbb","top margin:"+layoutParams.topMargin);
					indicatorGroup.setLayoutParams(layoutParams);
					indicatorGroup.requestLayout();
				}
				else if(null!=vlparams)
				{
					Log.i("bbb"," layout class:"+vlparams.getClass().toString());
				}
				else
				{
					Log.i("bbb"," layout class is null");
				}

				
			    if(indicatorGroup.getParent()==null){
					parentLayout.addView(indicatorGroup);
					Log.i("bbb"," add indicatorGroup to Parent");
				}
			    //parentLayout.updateViewLayout(indicatorGroup, layoutParams);
			 }
		}
		
		//如果当前的FixGroupView为第一个Group的话，则判断其是否展开并设置相应箭头图标
		if(FixGroupView!=null && indicatorGroupId==0)
		{
			ImageView iv=(ImageView) FixGroupView.findViewById(R.id.group_narrow_image);
			boolean isExpanded=this.isGroupExpanded(0);
			Bitmap bm;
			if(isExpanded)
			{
			bm = BitmapFactory.decodeStream(this.getResources().openRawResource(R.drawable.narrow_select));	
			}
			else
			{
			bm = BitmapFactory.decodeStream(this.getResources().openRawResource(R.drawable.narrow));
			}
			Bitmap newBm = MyGraphicMethod.scaleImg(bm , (int)(m_DeviceHeightPixel*IMAGE_SIZE_SCALE_GROUP), (int)(m_DeviceHeightPixel*IMAGE_SIZE_SCALE_GROUP));
			iv.setImageBitmap(newBm );
			
		}
		
		
		
		
		
		if (this.mOnScrollListener != null)
		      this.mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (this.mOnScrollListener != null)
		      this.mOnScrollListener.onScrollStateChanged(view, scrollState);
	}
	
	public void setOnScrollListener(AbsListView.OnScrollListener paramOnScrollListener)
	{
	    this.mOnScrollListener = paramOnScrollListener;
	}
    public void setAdapter(ExpandableListAdapter paramExpandableListAdapter)
    {
//	    int m = 0;
	    super.setAdapter(paramExpandableListAdapter);
//	    if (this.a != null)
//	    {
//	      FrameLayout localFrameLayout1 = this.a;
//	      FrameLayout localFrameLayout2 = this.f;
//	      localFrameLayout1.removeView(localFrameLayout2);
//	    }
//	    this.d = m;
//	    this.f = m;
    }
	  private FrameLayout parentLayout=null;  //listview所在的父层对象
	  private boolean isFixGroup=true;  //是否要固定显示组名在上边
	  private View FixGroupView=null;   //始终固定显示的组view
	  private ExpandableListAdapter mAdapter;
	  private AbsListView.OnScrollListener mOnScrollListener=null;
	  private int indicatorGroupId=-1; //当前固定显示的组索引
	  private int indicatorGroupHeight=0;
	  private LinearLayout indicatorGroup;
	  private Context mContext;
	  
	  
	  

}
